<?php
    include("../includes/config.php");
    include("../includes/connection.php");
    include("../includes/database.php");

    include("authenticate.php");
    
    $title = "Manage Posts";
    
    include("header.php")
?>

<h1>Manage Posts</h1>

<a href="add-post.php" class="btn btn-primary">Add New Post</a>

<table class="table">
    <thead>
        <tr>
            <th style="width: 10%;">No.</th>
            <th style="width: 40%;">Title</th>
            <th style="width: 20%;">Author</th>
            <th style="width: 30%;">Actions</th>            
        </tr>
    </thead>
    <tbody>
        <?php 
            $posts = $_SESSION["user_level"] == 1 ? getAllPosts() : getAllPostByAuthor($_SESSION["user_id"]);
            while ($post = mysql_fetch_assoc($posts)) {
        ?>
            <tr>
                <td><?php echo $post["post_id"] ?></td>
                <td><?php echo $post["title"] ?></td>
                <td><?php echo $post["author_name"] ?></td>
                <td>
                    <a href="edit-post.php?id=<?php echo $post["post_id"] ?>" class="btn">Edit</a>
                    <a href="delete-post.php?id=<?php echo $post["post_id"] ?>" class="btn btn-danger">Delete</a>
                </td>
            </tr>
        <?php
            }
        ?>
        <tr>
            <td><em>#</em></td>
            <td><em>Post Title Here</em></td>
            <td><em>Author Name Here</em></td>
            <td>
                <a href="#" class="btn">Edit</a>
                <a href="#" class="btn btn-danger">Delete</a>
            </td>
        </tr>        
    </tbody>
</table>
<?php
    include ("footer.php");
?>